package com.longnuoqi.mydoctor.adapter;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.longnuoqi.mydoctor.entity.Store;
import com.longnuoqi.mydoctor.utils.VolleyUtil;
import com.yunhe.mydoctor.R;

import java.util.List;


/**
 * Created by Lenovo on 2015/12/26.
 */
public class NearbyStoreListViewAdapter extends BaseAdapter {
    private List<Store> list;
    private Context context;
    private LayoutInflater inflater;
    private int id;
    private String name;
    private String address;
    private double x;
    private double y;
    private String url;

    public NearbyStoreListViewAdapter(Context context, List<Store> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.nearby_listview_item, null);
            holder.img = (ImageView) convertView.findViewById(R.id.nearby_lv_img);
            holder.name = (TextView) convertView.findViewById(R.id.nearby_lv_name_txt);
            holder.content = (TextView) convertView.findViewById(R.id.nearby_lv_content_txt);
            holder.address = (TextView) convertView.findViewById(R.id.nearby_lv_address_txt);
            holder.distance = (TextView) convertView.findViewById(R.id.nearby_lv_dis_txt);
            holder.watch = (TextView) convertView.findViewById(R.id.nearby_lv_watch_txt);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        id = list.get(position).getId();
        name = list.get(position).getName();
        address = list.get(position).getAddress();
        x = list.get(position).getX();
        y = list.get(position).getY();
        url = list.get(position).getImg();
        new VolleyUtil().getImageLoader(context, url, holder.img);

        Store store = new Store();
        store.setId(id);
        store.setName(name);
        store.setAddress(address);
        store.setX(x);
        store.setY(y);
        holder.name.setTag(store);
        holder.name.setText(name);
        holder.content.setText("经营类型：" + list.get(position).getBusiness());
        holder.address.setText(address);
        holder.distance.setText("300");
        holder.watch.setText("查看地图");
        return convertView;
    }

    private class ViewHolder {
        public ImageView img;
        public TextView name;
        public TextView content;
        public TextView address;
        public TextView distance;
        public TextView watch;
    }
}
